<?php
namespace adminmatrix\admin\command\swoole;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\facade\Console;
use think\swoole\Manager;

class Start  extends Command
{
    public function configure()
    {
        $this->setName('swoole:run')
            ->addOption(
                'env',
                'E',
                Option::VALUE_REQUIRED,
                'Environment name',
                ''
            )
            ->setDescription("swoolesssssssssssss");
//        parent::configure(); // TODO: Change the autogenerated stub
    }

    public function handle(Manager $manager)
    {
        $this->checkEnvironment();
        $echo = <<<EOT

███████╗██╗    ██╗ ██████╗  ██████╗ ██╗     ███████╗    ██████╗ ██╗   ██╗███╗   ██╗
██╔════╝██║    ██║██╔═══██╗██╔═══██╗██║     ██╔════╝    ██╔══██╗██║   ██║████╗  ██║
███████╗██║ █╗ ██║██║   ██║██║   ██║██║     █████╗      ██████╔╝██║   ██║██╔██╗ ██║
╚════██║██║███╗██║██║   ██║██║   ██║██║     ██╔══╝      ██╔══██╗██║   ██║██║╚██╗██║
███████║╚███╔███╔╝╚██████╔╝╚██████╔╝███████╗███████╗    ██║  ██║╚██████╔╝██║ ╚████║
╚══════╝ ╚══╝╚══╝  ╚═════╝  ╚═════╝ ╚══════╝╚══════╝    ╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═══╝

EOT;

        $this->output->write($echo);
        $this->output->writeln('Starting swoole server...');

        $this->output->writeln('You can exit with <info>`CTRL-C`</info>');

        $envName = $this->input->getOption('env');

        $manager->start($envName);
    }

    /**
     * 检查环境
     */
    protected function checkEnvironment()
    {
        if (!extension_loaded('swoole')) {
            $this->output->error('Can\'t detect Swoole extension installed.');

            exit(1);
        }

        if (!version_compare(swoole_version(), '4.6.0', 'ge')) {
            $this->output->error('Your Swoole version must be higher than `4.6.0`.');

            exit(1);
        }
    }
}